package ${webPackageName}.test;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import ${commonPackageName}.constant.Constant;
import ${commonPackageName}.utils.RedisUtil;
import ${servicePackageName}.dto.ResourceRoleTreeParentDto;
import ${servicePackageName}.dto.sys.LoginDto;
import ${servicePackageName}.dto.sys.UserDto;
import ${servicePackageName}.entity.sys.SysUser;
import ${servicePackageName}.mapstruct.SysUserConverMapper;
import ${servicePackageName}.service.sys.ISysResourceService;
import ${servicePackageName}.service.sys.ISysUserDepartmentService;
import ${servicePackageName}.service.sys.ISysUserService;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.IdUtil;

/**
 * 单元测试登录
 * @author lizihao
 *
 */
<#noparse>
public class TestFoo {
	protected MockMvc mvc;
	protected String header = "";
    @Autowired
    private WebApplicationContext wac;
    @Autowired
    private RedisUtil redisUtil;
	@Autowired
	private ISysUserService iUserService;
	@Autowired
	private ISysResourceService iResourceService;
	@Autowired
	private ISysUserDepartmentService iSysUserDepartmentService;
	
	
	/**
	 * 初始化用户登录
	 * @throws Exception
	 */
    @Before
    public void  setupMockMvc() throws Exception{
    	//初始化MockMvc对象
        mvc = MockMvcBuilders.webAppContextSetup(wac).build(); 
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_account", "admin");
        SysUser u = iUserService.getOne(queryWrapper);
        //多个角色的要去重路由
        List<ResourceRoleTreeParentDto> resourceList = iResourceService.selectUserResourceList(u.getId());
        //获取用户所在部门列表
        List<Long> departmentList = iSysUserDepartmentService.getUserDepatmentList(u.getId());
        String token =  IdUtil.randomUUID();
        assembleUser(u, token, resourceList, Convert.toList(String.class, departmentList));
        header = token;  
    }
    
    /**
     * 单元测试结束后清除登录令牌
     */
    @After
    public void afterRemove() {
    	System.out.println("清除令牌..."+header);
    	redisUtil.del(header);
    }
    
    /**
	 * 组装用户dto 
	 * @param u
	 * @param token
	 * @param resourceList
	 * @param departmentList
	 * @return
	 */
	private LoginDto assembleUser(SysUser u, String token, List<ResourceRoleTreeParentDto> resourceList,
			List<String> departmentList) {
			LoginDto dto = new LoginDto().setToken(token).setResourceList(resourceList);
		   //系统内部封装的用户信息
		   UserDto userDto = SysUserConverMapper.INSTANCES.sysUserToUserDto(u);
		   List<String> permissionList = new ArrayList<String>();
		   getNodes(permissionList, resourceList);
		   userDto.setResourceList(resourceList).setToken(token).setPermissionList(permissionList).setDepartmentList(departmentList);
		   redisUtil.set(token, userDto, Constant.TOKEN_TIME_OUT);
		return dto;
	}
    
    /**
	 * 递归获取所有父子节点
	 * @param permissionList
	 * @param resourceList
	 */
	private void getNodes(List<String> permissionList, List<ResourceRoleTreeParentDto> resourceList) {
		for(ResourceRoleTreeParentDto d : resourceList) {
			permissionList.add(d.getResourceCode());
			if(d.getChildren().size()!=0) {
				getNodes(permissionList, d.getChildren());
			}
		}
	}
}
</#noparse>